import XMRequest from './request'
import localCache from '../util/cache'
import { BASE_URL, TIME_OUT } from './request/config'

const xmRequest = new XMRequest({
  baseURL: BASE_URL,
  timeout: TIME_OUT,
  interceptors: {
    requestInterceptor: (config: any) => {
      const token = localCache.getCache('token')

      if (token) {
        config.headers!.Authorization = `Bearer ${token}`
      }
      return config
    },
    requestInterceptorCatch(err) {
      console.log('请求失败的栏截')

      return err
    },
    responseInterceptor(res) {
      console.log('响应成功的栏截')
      return res
    },
    responseInterceptorCatch(err) {
      console.log('响应失败的拦截')

      return err
    }
  }
})

export default xmRequest
